package cn.shop.cloud.web.controller;


import cn.shop.cloud.common.Result;
import cn.shop.cloud.domain.User;
import cn.shop.cloud.resolver.RedisValue;
import cn.shop.cloud.service.UserService;
import cn.shop.cloud.util.CookieUtil;
import cn.shop.cloud.web.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * @program: work
 * @ClassName TokenController
 * @description:
 * @author: 李尧坤
 * @create: 2024−12-19 20:18
 * @Version 1.0
 **/
@RestController
@RequestMapping("/token")
public class TokenController {

    @Autowired
    private UserService userService;

    //  url:"http://localhost:9000/member/token/login",
    @RequestMapping("/login")
    public Result<String> login(@Valid LoginVo loginVo, HttpServletResponse response){
        //登录方法
        String token = userService.login(loginVo);
        //把信息添加到cookie中  分布式session ===>  redis +token
        CookieUtil.addCookie(response,CookieUtil.TOKEN_COOKIE_NAME,token,CookieUtil.TOKEN_EXPIR_TIME);
        return Result.success(null);
    }

    // http://localhost:9000/member/token/getCurrent
//    @RequestMapping("/getCurrent")
//    //  required 默认值是 true  我们需要改为  false  走默认值 要求 请求中必须有cookie  没有就会报错  400
//    public Result<User> getCurrent(
//            @CookieValue(value = CookieUtil.TOKEN_COOKIE_NAME,required = false) String token){
//
//        User user=userService.getUserByToken(token);
//
//        System.out.println("------------------->"+user);
//
//        return  Result.success(user);
//
//    }

    @RequestMapping("/getCurrent")
    public Result<User> getCurrent(@RedisValue User user){

        System.out.println("------------------------");
        System.out.println("------------------->"+user);
        return  Result.success(user);
    }


}
